<?php
class signup_data extends data 
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function getEventListProgrammed() 
	{	
		$this->query("SELECT r.eventid as id,  r.eventname, i.istancename, r.startdate, r.enddate, r.duedate FROM event as r, istance as i WHERE r.istanceid = i.istanceid  and r.duedate > ".time()."");
		if($this->num_rows())
			return $this->fetch_all_row();
		else 
			return null;
	}
	
	public function getPgConfirmed($roleid)
	{
		
		
		if($roleid)
		{
			$this->query("SELECT s.signupid as id, p.pgname, c.classname, u.username, s.signuptime, r.rolename, s.signupemail as option_signupemail, s.signupcomment as option_signupcomment FROM signup s, pg p, user u, classes c, role r WHERE s.pgid=p.pgid AND s.userid=u.userid AND p.class = c.classid AND s.signuprole=r.roleid AND s.eventid={$_POST['eventid']} AND roleid=$roleid AND signupconfirmed = 1");
		}
		else
			$this->query("SELECT p.pgname, u.username FROM signup s, pg p, user u WHERE s.pgid=p.pgid and s.userid=u.userid and  s.eventid={$_POST['eventid']} AND signupconfirmed = 1");
		
		if($this->num_rows())
			return $this->fetch_all_row();
		else 
			return null;
	}
	
	public function getPGQueue($eventid)
	{
		$data_event = parent::getEventData($eventid);
		
		$res = $this->query("SELECT s.signupid as id, s.pgid, p.pgname, c.classname, u.username, s.signuptime, r.rolename, s.signuprole, s.signupemail as option_signupemail, s.signupcomment as option_signupcomment FROM signup s, pg p, user u, classes c, role r WHERE s.pgid=p.pgid AND s.userid=u.userid AND p.class = c.classid AND s.signuprole=r.roleid AND s.eventid={$_POST['eventid']} AND s.signupqueue = 1");

		if($this->num_rows($res))
		{
			while(list($id, $pgid, $pgname, $classname, $username, $signuptime, $rolename, $roleid, $optionemail, $optioncomment) = $this->fetch_row($res))
			{
				$res2 = $this->query("SELECT p.roleid, r.rolename FROM pgrole p, role r WHERE p.roleid=r.roleid AND p.pgid=$pgid ORDER BY main DESC");
				$listrole = $this->fetch_combo_all_row($res2);

				$listPgQueue[] = array('id'=>$id,'pgname'=>$pgname, 'class'=>$classname, 'username'=>$username, 'signuptime'=>$signuptime, 'rolesignup'=>$rolename, 'role'=>$listrole, 'option_signupemail'=>$optionemail, 'option_signupcomment'=>$optioncomment);
				
			}

			return $listPgQueue;
		}
		else 
			return null;
	}
	
	public function getPgOut()
	{
		$this->query("SELECT p.pgname, u.username FROM signup s, pg p, user u WHERE s.pgid=p.pgid and s.userid=u.userid and s.eventid={$_POST['eventid']} and signupout = 1");
		if($this->num_rows())
			return $this->fetch_all_row();
		else 
			return null;
	}
	
	public function setSignup()
	{	
		if($_POST['signupstatus'] == $this->getStatusConfirmed())	
			$query_signup_insert = " signupconfirmed = 1";
		elseif($_POST['signupstatus'] == $this->getStatusOut())
			$query_signup_insert = " signupout = 1";
		elseif($_POST['signupstatus'] == $this->getStatusQueue())
			$query_signup_insert = " signupqueue = 1";
			
		if(!$this->query("INSERT INTO signup SET eventid={$_POST['eventid']}, pgid={$_POST['pgid']}, userid={$_SESSION['userid']}, signuprole={$_POST['signuprole']}, signupemail='{$_POST['checkemail']}', signuptime=".time().", signupcomment='{$_POST['signupcomment']}', $query_signup_insert",0,1))
			return $this->error;
	}
	
	
	
	public function setSignupOut()
	{
		if(!$this->query("UPDATE signup SET signupout = 1, signupqueue = 0, signupconfirmed = 0 WHERE signupid = {$_POST['signupid']} "))
			return $this->error;
	}
	
	public function setSignupConfirmedMainRole()
	{
		if(!$this->query("UPDATE signup SET signupout = 0, signupqueue = 0, signupconfirmed = 1 WHERE signupid = {$_POST['signupid']} "))
			return $this->error;		
	}
	
	public function setSignupConfirmedSecondrole()
	{
		$this->query("SELECT pgid FROM signup WHERE signupid = {$_POST['signupid']}");
		list($pgid) = $this->fetch_row();
		$this->query("SELECT roleid FROM pgrole WHERE pgid=$pgid and main=0");
		if($this->num_rows())
		{
			list($role)=$this->fetch_row();
			if(!$this->query("UPDATE signup SET signuprole=$role, signupout = 0, signupqueue = 0, signupconfirmed = 1 WHERE signupid = {$_POST['signupid']} "))
				return $this->error;		
		}
	}
		
	public function getPgAllowOnEvent($eventid)
	{
		$data_event = parent::getEventData($eventid);
		
		if($data_event['duedate'] > time())
		{
		
			if($data_event['onlymain'] == 1)
				$queryOnlyMain = " AND pgmain = 1";
			
			$this->query("SELECT pgid FROM signup WHERE eventid = $eventid");
			while(list($pgoneventid) = $this->fetch_row())
				$pgonevent[] = $pgoneventid;
			
			$this->query("SELECT pgid, pgname FROM pg WHERE userid = {$_SESSION['userid']}".$queryOnlyMain);
			while(list($pgid, $pgname) = $this->fetch_row())
			{
				if(is_array($pgonevent))
				{
					if(!in_array($pgid,$pgonevent))
						$databoxreturn[$pgid]=$pgname;
				}
				else 
				{
					$databoxreturn[$pgid]=$pgname;
				}
			}
		}	
		if($databoxreturn)
			return $databoxreturn;
		else 
			return null;
		
	}
	
	public function getPgRoleOnEvent()
	{
		$event_data = parent::getEventData($_POST['eventid']);
		if($event_data['onlyprimaryspec'] == 1)
		{
			$query = " AND main = 1";
		}
		
		$this->query("SELECT p.roleid, r.rolename FROM pgrole p, role r WHERE p.roleid=r.roleid AND p.pgid={$_POST['pgid']} $query ORDER BY main DESC");
		return $this->fetch_all_row();		
	}
	
}
?>